package utils

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"
	"os/exec"
	"runtime"
)

func Struct2JsonString(o interface{}) string {
	b, err := json.Marshal(o)
	if err != nil {
		return fmt.Sprintf("%+v", o)
	}
	var out bytes.Buffer
	err = json.Indent(&out, b, "", "  ")
	if err != nil {
		return fmt.Sprintf("%+v", o)
	}
	return out.String()
}

func KillProcess(pid int) {
	var c *exec.Cmd
	strPid := fmt.Sprintf("%d", pid)
	sysType := runtime.GOOS
	switch sysType {
	case "linux":
		c = exec.Command("kill", "-9", strPid)
	case "windows":
		c = exec.Command("taskkill", "/f", "/pid", strPid)
	default:
		log.Println("GOOS ", sysType, " is unknown!!")
		return
	}

	c.Run()
}
